package com.yellowpage.white.ui;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpRequest;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.yellowpage.white.util.DomParser;

public class XMLMenuImpl implements Menu {

	public List<MenuItem> getMenuItemList() {
		return menuItemList;
	}

	private String menuFilePath; // 配置文件的路径
	private List<MenuItem> menuItemList = new ArrayList();
	List functionList  = new ArrayList();
	
	private HttpServletRequest request = null;

	private Document doc;
	
	
	public XMLMenuImpl() {
		
		InputStream input = this.getClass().getResourceAsStream("/menu.xml");
		parsingMenuFile((new DomParser(input)).getDocument());
	}
	

	public XMLMenuImpl(HttpServletRequest req) {
		this.request = req;
		InputStream input = this.getClass().getResourceAsStream("/menu.xml");
		parsingMenuFile((new DomParser(input)).getDocument());
	}
	
	private void parsingMenuFile(Document doc) {	
		
		
		//functionList = (List)session.getAttribute("FUNC_LIST");       
		
		menuItemList = new ArrayList();
		Element menuElem = doc.getDocumentElement();
		// NodeList itemElems = menuElem.getElementsByTagName("item");
		NodeList itemElems = menuElem.getChildNodes();

		for (int i = 0; i < itemElems.getLength(); i++) {

			Node node = (Node) itemElems.item(i);
			
		
			if (node.getNodeType() == Node.ELEMENT_NODE) {

				Element itemElem = (Element) node;
				
				menuItemList.add(parsingMenuItem(itemElem));
			}
		}

	}
	
	private String getUserRoles(){
		
		return null;
	}

	private MenuItem parsingMenuItem(Element itemElem) {
		XMLMenuItemImpl menuItem = new XMLMenuItemImpl();

		menuItem.setId(itemElem.getAttribute("id"));
		menuItem.setName(itemElem.getAttribute("name"));
		menuItem.setDistination(itemElem.getAttribute("distination"));
		menuItem.setFunction(itemElem.getAttribute("function"));
		menuItem.setImg(itemElem.getAttribute("img"));		
		
		
		String func = itemElem.getAttribute("function");
		
		System.out.println("******** parsingMenuItem subNode:" + itemElem.getAttribute("name"));
		
		if(request.isUserInRole(func)){
			menuItem.setVisible(true);
		}else{			
			menuItem.setVisible(false);
		}

		NodeList subNodes = itemElem.getChildNodes();

		List subMenuItems = new ArrayList();
		for (int i = 0; i < subNodes.getLength(); i++) {
			Node subNode = subNodes.item(i);			
			

			if (subNode.getNodeType() == Node.ELEMENT_NODE) {

				Element subItemElem = (Element) subNode;				
				
				subMenuItems.add(parsingMenuItem(subItemElem));
			}
		}
		menuItem.setSubMenuItems(subMenuItems);

		return menuItem;
	}

	// 测试方法
	public static void main(String[] args) {
//		Menu menu = new XMLMenuImpl();
//
//		List items = menu.getMenuItemList();
//
//		System.out.println("MenuSize ::" + items.size() + "");
//
//		for (int i = 0; i < items.size(); i++) {
//			MenuItem item = (MenuItem) items.get(i);
//			System.out.println("    MenuItem ::" + item.getId() + "");
//
//			List subItems = item.getSubMenuItems();
//			for (int j = 0; j < subItems.size(); j++) {
//				MenuItem subItem = (MenuItem) subItems.get(j);
//				System.out.println("    SubItem ::" + subItem.getId() + "");
//			}
//		}

	}

}
